<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;
class Bonus extends Common
{
    //列表页
    public function index()
    {
        //接收参数 page order
        if(empty($_GET['page']) || !isset($_GET['page']) || $_GET['page']=='' || $_GET['page']==null || $_GET['page']<1){
            $_GET['page']=1;
        }
        if(empty($_GET['order']) || !isset($_GET['order']) || $_GET['order']=='' || $_GET['order']==null || $_GET['order']<1){
            $_GET['order']='';
        }
//        if(empty($_GET['type']) || !isset($_GET['type']) || $_GET['type']=='' || $_GET['type']==null || $_GET['type']<1){
//            $_GET['type']=array(1,2,3,4,5);
//        }
//        if(empty($_GET['search']) || !isset($_GET['search']) || $_GET['search']=='' || $_GET['search']==null){
//            $_GET['search']='';
//        }
//        if(empty($_GET['completion']) || !isset($_GET['completion']) || $_GET['completion']=='' || $_GET['completion']==null|| $_GET['completion']<1){
//            $_GET['completion']=array(1,2,3);
//        }

        //查询条件
        $where=array(
            array( 'order','like','%'. $_GET['order'].'%'),
            array('delete_time','=',0),
            array('is_completion','>',1)
        );

        //查询
        $res = Db::table('xt_project')
            ->where($where)
            ->field('id,order,title,capital,income,material,personnel,tax,risk,develop,profit,bonus,percent,contract_amount')
            ->order('update_time desc')
            ->paginate(10);
//dump($res);
//        die();
        $this->assign('res',$res);
        return view();
    }


    //更新
    public function edit(){
        $res = Db::table('xt_project')->where('id',$_GET['id'])->find();
        $this->assign('res',$res);
        return view();
    }
    //做更新
    public function doEdit(){

        $_POST['profit'] = $_POST['income']-$_POST['material']-$_POST['personnel']-$_POST['tax']-$_POST['capital']-$_POST['risk'];
        $_POST['bonus'] = $_POST['profit']*$_POST['percent']/100.0;

        $res=Db::table('xt_project')->update($_POST);
//        dump($res);
        if ($res==1){
            $rt=array(
                'code'=>1,
                'msg'=>'更新成功',
            );
        }else{
            $rt=array(
                'code'=>-10002,
                'msg'=>'未发生变化，请检查更新的数据',
            );
        }
        echo json_encode($rt);
    }

}